<template>
  <div class="content">
    <UiHeader class="ui-header" :showBanner="false" />
    <img class="banner" src="@/assets/list/banner.png" />
    <div class="ui-center">
      <div class="search-box">
        <input type="text" v-model="searchStr" placeholder="请输入关键词搜索" @keyup.enter="GetData" />
        <div class="btn">
          <i class="icon"></i>
          <span @click="GetData">搜索</span>
        </div>
      </div>
      <div class="tabs-box ui-flex align-center justify-center">
        <template v-for="(item, index) in tabs" :key="item.id">
          <div :class="['item', { on: index === active }]" @click="Change(index)">{{ item.label }}</div>
        </template>
      </div>
      <div class="result-box ui-flex flex-wrap" v-if="active === 0 || active === 1">
        <div class="item" v-for="item in list" :key="item.id" @click="JumpDetail(item)">
          <img :src="baseURL + item.coverImg" alt="" />
          <p class="title" :title="item.title">{{ item.title }}</p>
        </div>
      </div>
      <div class="list ui-center" v-else>
        <div class="item ui-flex align-center" v-for="item in list" :key="item.id" @click="JumpDetail(item)">
          <div class="title flex-1 ui-oneline">{{ item.title }}</div>
          <div class="date">{{ item.createTime }}</div>
          <i class="icon"></i>
        </div>
      </div>
      <div class="page-box ui-flex justify-center" v-if="totalPage">
        <div class="item" @click="JumpPage('first')">首页</div>
        <div class="item" @click="JumpPage('-1')">上一页</div>
        <div class="item">第{{ page + 1 }}/{{ totalPage }}页</div>
        <div class="item" @click="JumpPage('1')">下一页</div>
        <div class="item" @click="JumpPage('last')">尾页</div>
      </div>
    </div>
    <UiFooter />
  </div>
</template>

<script setup>
const { get, baseURL, router } = inject('global');

let active = ref(0),
  tabs = ref([]);
const Change = index => {
  active.value = index;
  page.value = 0;
  GetData();
};
const GetGate = () => {
  get('/dict', { blurry: 'global_search_category' }).then(res => {
    tabs.value = res.content[0]?.dictDetails;
    GetData();
  });
};

let list = ref([]),
  page = ref(0),
  totalPage = ref(0),
  searchStr = ref('');
const GetData = () => {
  list.value = [];
  get('/global/search', { category: tabs.value[active.value].value, page: page.value, size: 16, keyword: searchStr.value }).then(res => {
    list.value = res[res.category].content;
    totalPage.value = Math.ceil(res[res.category].totalElements / 16);
  });
};
const JumpPage = control => {
  if (control === 'first') {
    page.value = 0;
  } else if (control === 'last') {
    page.value = totalPage.value - 1;
  } else if (control === '-1') {
    if (page.value <= 0) {
      return;
    }
    page.value += parseInt(control);
  } else if (control === '1') {
    if (page.value >= totalPage.value - 1) {
      return;
    }
    page.value += parseInt(control);
  } else {
    return;
  }
  if (page.value < 0) {
    page.value = 0;
  }
  GetData();
};

const JumpDetail = data => {
  let current = tabs.value[active.value].value;
  if (current === 'textbook') {
    window.open(baseURL + data.fileUrl);
  } else {
    router.push('/listNewsDetail?id=' + data.id);
  }
};

watch(
  () => router.currentRoute.value.query,
  () => {
    searchStr.value = router.currentRoute.value.query.searchStr || '';
    GetData();
  }
);

onMounted(() => {
  searchStr.value = router.currentRoute.value.query.searchStr || '';
  GetGate();
});
</script>

<style lang="scss" scoped>
.content {
  min-height: 100vh;
  background-color: #ede8e2;
}
.banner {
  width: 100%;
  height: 250px;
  object-fit: cover;
  display: block;
}
.ui-center {
  overflow: hidden;
  min-height: calc(100vh - 618px);
}
.search-box {
  margin: 40px auto;
  width: 600px;
  padding: 10px;
  background: #e5dcd1;
  border: 1px solid #bcb3b0;
  border-radius: 4px;
  display: flex;
  align-items: center;
  &.on {
    border-color: #9c2c29;
  }
  input {
    background-color: transparent;
    border: none;
    outline: none;
    flex: 1;
    height: 28px;
    line-height: 28px;
    border-right: 1px solid #bcb3b0;
    padding: 4px 10px;
    box-sizing: border-box;
    color: #3d3d3d;
    font-size: 14px;
    &::placeholder {
      color: #7c715f;
    }
  }
  .btn {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #9c2c29;
    line-height: 20px;
    padding: 0 20px;
    cursor: pointer;
    .icon {
      margin: 0 10px 0 0;
      width: 18px;
      height: 18px;
      @include bgImg('@/assets/search_red.png');
    }
  }
}
.tabs-box {
  border-bottom: 1px solid #d3b491;
  .item {
    font-size: 18px;
    color: #7c715f;
    padding: 20px 40px;
    border-bottom: 1px solid transparent;
    line-height: 26px;
    position: relative;
    cursor: pointer;

    &:not(:last-child) {
      margin-right: 10px;

      &::after {
        position: absolute;
        top: 50%;
        right: 0;
        transform: translate(100%, -50%);
        content: '';
        display: block;
        width: 10px;
        height: 26px;
        @include bgImg('@/assets/list/tabs-divider.png');
        flex-shrink: 0;
      }
    }
    &.on {
      color: #9c2c29;
      font-size: 18px;
      font-weight: bold;
      border-color: #9c2c29;
    }
  }
}
.result-box {
  padding: 40px 0 0;
  gap: 25px;
  .item {
    cursor: pointer;
    padding: 4px;
    width: 300px;
    box-sizing: border-box;
    border: 1px solid #9c2c29;
    background: #fffdf7;
    box-shadow: 0px 5px 10px 0px rgba(0, 0, 0, 0.3);
    img {
      width: 100%;
      height: 236.62px;
      object-fit: cover;
    }
    .title {
      margin: 10px 0 0 0;
      font-size: 14px;
      color: #313131;
      line-height: 28.79px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      text-align: center;
    }
  }
}
.list {
  padding: 30px 0 0 0;
  .item {
    font-size: 14px;
    color: #3d3d3d;
    line-height: 22px;
    cursor: pointer;
    border-bottom: 1px dashed #c5c5c5;
    .title {
      min-width: 0;
      padding: 19.5px 0;
    }
    .date {
      padding: 19.5px 0 19.5px 10px;
    }
    .icon {
      width: 120px;
      height: 61px;
      @include bgImg('@/assets/list/arrow.png');
    }
  }
}

.page-box {
  margin: 50px 0;
  .item {
    cursor: pointer;
    margin: 0 10px 0 0;
    font-size: 14px;
    color: #9e9e9e;
    line-height: 20px;
    padding: 10px 20px;
    border: 1px solid #dddddd;

    &:last-child {
      margin: 0;
    }
  }
}
</style>
